<?php
/*
 * @Author: your name
 * @Date: 2019-12-21 15:54:06
 * @LastEditTime: 2020-06-20 15:08:27
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /ahsmp_3/application/asmpasync/controller/Async.php
 */
namespace app\asmpasync\controller;

use think\facade\Request;

class Async
{
    /**
     * @name: 
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-26 17:30:15
     */
    function callClient ()
    {
        $module = Request::Post('module');
        $action = Request::Post('action');
        $params_json = Request::Post('params');

        $params = json_decode($params_json, true);
//        echo json_encode($params);die;
        $userId = Request::Post('user_id');
        $host_id_a = Request::Post('host_id');
        $type = '\app\asmpasync\service\\' . $module; 
        $date = date('Y-m-d H:i:s');
        file_put_contents('../runtime/async.log', $date . '-checkClass-' . json_encode($_POST) . PHP_EOL, FILE_APPEND);
        if(!class_exists($type))
        {
            die('不存在该异步类');
        }
        file_put_contents('../runtime/async.log', $date . '-checkAction-' . PHP_EOL, FILE_APPEND);
        $Class = new $type();
        if(!method_exists($Class, $action))
        {
            die('不存在该异步方法');
        }
        file_put_contents('../runtime/async.log', $date . '-checkHostId-' . PHP_EOL, FILE_APPEND);
        if(empty($host_id_a))
        {
            die('未找到主机id');
        }

        $hostLink = db('HostLink')->whereIn('host_id', $host_id_a)->where('module', $module)->select();

        file_put_contents('../runtime/async.log', $date . '-checkHostLik-' . json_encode($hostLink) . PHP_EOL, FILE_APPEND);
        try{
            foreach ($hostLink as $v)
            {
                $tmp = [$v['host_id']];
                $tmp = array_merge($tmp, $params);

                //获取数据
                $msg = call_user_func_array([$Class, $action], $tmp);

                $SocketPackage = new \app\asmpasync\socket\SocketPackage();
                $SocketPackage->host_id = $v['host_id'];//主机id
                $SocketPackage->module = $v['module'];//模块指向


                //打包数据
                $data = $SocketPackage->dataPackage($msg);
                $SocketClient = new \app\asmpasync\socket\SocketClient($v['ip'], $v['port']);

                //发送数据
                $SocketClient->sendMsg($data);
                $base64Data = base64_encode($data);
                $messageData = [
                    'host_id' => $v['host_id'],
                    'user_id' => $userId,
                    'description' => $Class->classDescription . $Class->actionDescription,
                    'content' => $base64Data,
                    'content_md5' => md5($base64Data),
                    'module' => $v['module'],
                    'err_id' => $SocketClient->error_id,
                    'err_msg' => $SocketClient->err_msg,
                    'ctime' => time(),
                    'ip' => $v['ip'],
                    'port' => $v['port'],
                ];
                db('ServerSocketMessageHistory')->insert($messageData);
            }
        }catch (\Exception $e){
            file_put_contents('../runtime/async.log', $date . $e->getMessage() . '--file:'. $e->getFile() . '--line:' .  $e->getLine() . PHP_EOL, FILE_APPEND);
        }

        file_put_contents('../runtime/async.log', $date . '-SUCCESS-' . PHP_EOL, FILE_APPEND);
    }
}